<?php


namespace app\admin\business;


use app\common\model\Admin;
use app\common\model\Rule;
use think\facade\View;

class adminBus
{
    public $AdminMode = null;
    public $RuleModel = null;
    public function __construct()
    {
        $this->AdminMode = new \app\common\model\Admin();
        $this->RuleModel = new Rule();
    }
    public function loginBus($adminData){
        $admin = new Admin();
        $result = $admin->getAdminUserByName($adminData['name']);
        if(empty($result)) {
            throw new \think\Exception('不存在该用户');
        }
        if($result['password'] != md5($adminData['password'].'_breeze')){
            throw new \think\Exception('密码错误');
        }
        $updateData = [
            'last_login_ip' => request()->ip(),
            'update_time' => time()
        ];

        $res = $admin->updateById($result['id'],$updateData);
        if( empty($res) ){
            throw new \think\Exception('登录失败，更新数据未成功');
        }
        $setSessionData=[
            'name'=>$result['name'],
            'id' => $result['id'],
            'roleID'=> $result['roleID'],
            'roleName'=> $result['roleName']
        ];
        session(config('admin.session_admin'),$setSessionData);
        return true;
    }
    public function getAdminListBus(){
        $result = $this->AdminMode->getList();
        if(empty($result)){
            throw new \think\Exception('未查询到数据');
        }
        return $result;
    }
    public function getAdminByIdBus($id){
        $result = $this->AdminMode->getDetailById($id);
        if(empty($result)){
            throw new \think\Exception('未查询到数据');
        }
        return $result;
    }
    public function getRuleListBus(){
        $result = $this->RuleModel->getList();
        if(empty($result)){
            throw new \think\Exception('未查询到数据');
        }
        return $result;
    }
    public function updateAdminByWhereBus($where,$data){
        $result = $this->AdminMode->changeFieldById($where,$data);
        if(empty($result)){
            throw new \think\Exception('修改错误，请检查数据');
        }
        return $result;
    }
    public function addAdminBus($data){
        $data['password'] = md5($data['password'].'_breeze');
        $result = $this->AdminMode->add($data);
        if(empty($result)){
            throw new \think\Exception('添加错误，请检查数据');
        }
        return $result;
    }
    public function setPasswordByWhereBus($id,$password){
        $where = [
            'id'=>$id,

        ];
        $data = [
            'password' => md5($password.'_breeze')
        ];

        $result = $this->AdminMode->changeFieldById($where,$data);
        if(empty($result)){
            throw new \think\Exception('设置失败，请检查数据');
        }
        return $result;
    }
    public function removeByWhereBus($where){
        $res = $this->AdminMode->removeByWhere($where);
        if (empty($res)){
            throw new Exception('删除失败');
        }
        return $res;
    }
}